<%
# Copyright (C) 2013 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  module_item_next ||= nil
  module_item ||= module_item_next
  item_data ||= { published_status: 'unpublished' }
  completion_criteria ||= nil
  editable ||= false
  criterion = completion_criteria && completion_criteria.find{|c| c[:id] == module_item.id}

  @module_item_image_tags ||= {
    'indent' => "<i class='icon-arrow-right'></i>".html_safe,
    'outdent' => "<i class='icon-arrow-left'></i>".html_safe,
    'edit' => "<i class='icon-edit'></i>".html_safe,
    'delete' => "<i class='icon-end'></i>".html_safe,
    'duplicate' => "<i class='icon-copy-course'></i>".html_safe,
  }

  menu_type_to_class = {
    :assignment_menu => Assignment,
    :discussion_topic_menu => DiscussionTopic,
    :file_menu => Attachment,
    :quiz_menu => Quizzes::Quiz,
    :wiki_page_menu => WikiPage
  }
%>

<li
  id="context_module_item_<%= module_item ? module_item.id : "blank" %>"
  style="<%= hidden unless module_item %>"
  class="
    context_module_item
    <%= 'student-view' if @is_student %>
    <%= module_item.content_type_class if module_item %>
    <%= 'also_assignment' if module_item && module_item.graded? %>
    indent_<%= module_item.try_rescue(:indent) || '0' %>
    <%= 'progression_requirement' if criterion %>
    <%= criterion[:type] if criterion %>_requirement
    <%= module_item.item_class if module_item %>
    <%= 'dupeable' if module_item&.duplicate_able? %>
  "
>
  <div class="ig-row <%= 'with-completion-requirements' if criterion %> <%= 'ig-published' if module_item && module_item.published? %> <%= 'student-view' if @is_student %>">
    <a
      aria-label='<%= module_item && module_item.title %>'
      tabindex="-1"
      class="for-nvda"
      href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>">
      <%= module_item && module_item.title %>
    </a>

    <% if editable || module_item.nil? %>
    <div aria-hidden="true" class="ig-handle">
      <span
        class="draggable-handle move_item_link"
        title="{{#t}}Drag to reorder or move item to another module{{/t}}"
      >
        {{> icons/drag-handle}}
      </span>
    </div>
    <% end %>

    <span class="type_icon" title="<%= module_item_translated_content_type(module_item) %>">
      <span class="ig-type-icon">
        {{> icons/document}}
        {{> icons/download}}
        {{> icons/discussion}}
        {{> icons/assignment}}
        {{> icons/quiz}}
        {{> icons/link}}
      </span>
    </span>

    <div class="ig-info">
      <div class="module-item-title">
        <span class="item_name">
          <% if module_item && module_item.content_type == 'ExternalUrl' && module_item.new_tab %>
            <a
              title="<%= module_item && module_item.title %>"
              class="title external_url_link"
              target="_blank"
              href="<%= module_item.url %>"
              data-item-href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item.id %>"
            >
               <%= module_item.title %>
            </a>
          <% else %>
            <a
              title="<%= module_item && module_item.title %>"
              class="ig-title title"
              href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>"
              <% if item_data[:mastery_paths] && item_data[:mastery_paths][:locked] %>
              aria-describedby="module-item-locked-<%= module_item && module_item.id %>"
              <% end %>
            >
              <%= module_item && module_item.title %>
            </a>
          <% end %>
          <span title="<%= module_item && module_item.title %>" class="title locked_title"><%= module_item && module_item.title %></span>
          <span class="points_possible" style="display: none;"><%= round_if_whole(module_item.try_rescue(:assignment).try_rescue(:points_possible)) || nbsp %></span>
          <span class="requirement" style="display: none;">&nbsp;</span>
          <span class="completion_requirement" style="display: none;">&nbsp;</span>
          <span class="position" style="display: none;"><%= module_item && module_item.position %></span>
          <span class="url" style="display: none;"><%= module_item.try(:url) if editable %></span>
          <span class="new_tab" style="display: none;"><%= module_item.try(:new_tab) ? '1' : '0' if editable %></span>
        </span>
      </div>

      <div class="module_item_icons nobr">
        <!-- dead code? -->
        <span class="criterion <%= 'defined' if criterion %>">
          <span class="min_score" style="display: none;"><%= (criterion && criterion[:min_score]) || nbsp %></span>
          <span class="criterion_type" style="display: none;"><%= (criterion && criterion[:type]) || nbsp %></span>
        </span>
        <!-- /dead code -->
        <span class="type" style="display: none;"><%= module_item ? module_item.content_type_class : nbsp %></span>
        <span class="id" style="display: none;"><%= module_item ? module_item.id : nbsp %></span>
        <span class="graded" style="display: none;"><%= module_item && module_item.graded? ? '1' : '0' %></span>
      </div>

      <div class="ig-details">
        <div class="due_date_display ig-details__item"></div>
        <div class="points_possible_display ig-details__item"></div>
        <div class="requirement-description ig-details__item">
          <span class="completion_requirement">
            <span class="requirement_type min_score_requirement">
              <% score_display = content_tag(:span, criterion && criterion[:min_score] || nbsp, class: 'min_score') %>
              <span class="unfulfilled">
                <%= t 'min_score.unfulfilled', 'Score at least %{score}', :score => score_display %>
                <span class="screenreader-only"><%= t 'Must score at least %{score} to complete this module item', :score => score_display %></span>
              </span>
              <span class="fulfilled">
                <%= t 'min_score.fulfilled', 'Scored at least %{score}', :score => score_display %>
                <span class="screenreader-only"><%= t 'Module item has been completed by scoring at least %{score}', :score => score_display %></span>
              </span>
            </span>
            <span class="requirement_type must_view_requirement">
              <span class="unfulfilled">
                {{#t}}View{{/t}}
                <span class="screenreader-only">{{#t}}Must view in order to complete this module item{{/t}}</span>
              </span>
              <span class="fulfilled">
                {{#t}}Viewed{{/t}}
                <span class="screenreader-only">{{#t}}Module item has been viewed and is complete{{/t}}</span>
              </span>
            </span>
            <span class="requirement_type must_mark_done_requirement">
              <span class="unfulfilled">
                <%= t 'Mark done' %>
                <span class="screenreader-only"><%= t 'Must mark this module item done in order to complete' %></span>
              </span>
              <span class="fulfilled">
                <%= t 'Marked done' %>
                <span class="screenreader-only"><%= t 'Module item marked as done and is complete' %></span>
              </span>
            </span>
            <span class="requirement_type must_contribute_requirement">
              <span class="unfulfilled">
                {{#t}}Contribute{{/t}}
                <span class="screenreader-only">{{#t}}Must contribute to this module item to complete it{{/t}}</span>
              </span>
              <span class="fulfilled">
                {{#t}}Contributed{{/t}}
                <span class="screenreader-only">{{#t}}Contributed to this module item and is complete{{/t}}</span>
              </span>
            </span>
            <span class="requirement_type must_submit_requirement">
              <span class="unfulfilled">
                {{#t}}Submit{{/t}}
                <span class="screenreader-only">{{#t}}Must submit this module item to complete it{{/t}}</span>
              </span>
              <span class="fulfilled">
                {{#t}}Submitted{{/t}}
                <span class="screenreader-only">{{#t}}Module item submitted and is complete{{/t}}</span>
              </span>
            </span>
          </span>
        </div> <!-- requirement description end -->
      </div>
    </div>
    <% if !editable %>
      <div class="module-item-status-icon"></div> <%# going to set this in JS-land, not here %>
    <% end %>
    <% if editable || module_item.nil? %>
      <div class="ig-admin">
        <span
          data-module-item-id="<%= module_item && module_item.id %>"
          data-module-type="<%= module_item && module_item.content_type_class %>"
          data-content-id="<%= module_item && module_item.content_id %>"
          class="lock-icon"
        >
        </span>
        <span
          data-module-item-name="<%= module_item && module_item.title %>"
          data-module-type="<%= module_item && module_item.content_type_class %>"
          data-content-id="<%= module_item && module_item.content_id %>"
          data-id="<%= module_item_publishable_id(module_item) %>"
          data-course-id="<%= module_item && module_item.context_id %>"
          data-module-id="<%= module_item && module_item.context_module_id %>"
          data-module-item-id="<%= module_item && module_item.id %>"
          data-assignment-id="<%= module_item && module_item.assignment.try(:id) %>"
          data-is-cyoeable="<%= module_item && cyoe_able?(module_item) %>"
          data-published="<%= module_item && item_data[:published_status] == 'published' %>"
          data-publishable="<%= module_item_publishable?(module_item) %>"
          data-unpublishable="<%= module_item_unpublishable?(module_item) %>"
          data-publish-title="<%= module_item && module_item.title ? module_item.title : '' %>"
          title=""
          data-tooltip
          class="publish-icon <%= item_data[:published_status] %>"
        >
          {{> icons/<%=}}
        </span>

        <div class="inline-block cog-menu-container">
          <a class="al-trigger al-trigger-gray" role="button" tabindex="0" href="#">
            {{> icons/settings}}
            <span class="screenreader-only"><%= t('Manage %{item_name}', {item_name: module_item && module_item.title ? module_item.title : 'item'})  %></span>
          </a>

          <ul class="al-options">
            <li role="presentation"><a href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>" class="outdent_item_link" title="{{#t}}Decrease indent') %>"><%= @module_item_image_tags['outdent'] %> <%= t('Decrease indent{{/t}}</a></li>
            <li role="presentation"><a href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>" class="indent_item_link" title="{{#t}}Increase indent') %>"><%= @module_item_image_tags['indent'] %> <%= t('Increase indent{{/t}}</a></li>
            <li role="presentation"><a href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>" class="edit_item_link edit_link" title="{{#t}}Edit item details') %>"><%= @module_item_image_tags['edit'] %> <%= t('Edit{{/t}}</a></li>
            <li class="dupe-option" role="presentation"><a href="/api/v1<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>/duplicate" class="duplicate_item_link" title="{{#t}}Duplicate Item') %>"><%= @module_item_image_tags['duplicate'] %> <%= t('Duplicate{{/t}}</a></li>
            <li role="presentation">
              <a href="#<%= module_item ? module_item.id : "{{ id }}" %>"
                 aria-label="{{#t}}Move this item{{/t}}"
                 class="move_module_item_link icon-updown"
                 title="{{#t}}Move this item{{/t}}"
              >{{#t}}Move to...{{/t}}</a>
            </li>
            <li role="presentation"><a href="<%= context_url(@context, :context_url) %>/modules/items/<%= module_item ? module_item.id : "{{ id }}" %>" class="delete_item_link delete_link" title="{{#t}}Remove this item from the module') %>">{{> icons/trash}} <%= t('Remove{{/t}}</a></li>
            <% menu_type_to_class.each do |menu_type, content_class| %>
              <% if !module_item || module_item.content.is_a?(content_class) %>
                <%
                   launch_options = {}
                   if menu_type == :file_menu
                     launch_options[:files] = [module_item ? module_item.content_id : "{{ content_id }}"]
                   else
                     launch_options[:module_items] = [module_item ? module_item.id : "{{ id }}"]
                   end
                %>
                <%= external_tools_menu_items(@menu_tools[menu_type], {link_class: "menu_tool_link", settings_key: menu_type, in_list: true, url_params: launch_options}) %>
              <% end %>
            <% end %>
          </ul>
        </div>
      </div>
    <% end %>
  </div>
</li>
